package com.mflex.shelflocationquery.utils;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

import cz.msebera.android.httpclient.HttpEntity;

public class MyCLient {
    private static final String TAG = "MyCLient";
    //    private static final String BASE_URL = "http://ot-mfcsmtandonapi.mflex.com.cn";
//    private static final String BASE_URL = "http://mfcsmtandonapi.mflex.com.cn";
    private static final String BASE_URL = "http://10.1.34.63/BatchScanFun/";
//    private static final String TROUBLE_URL = BuildConfig.TROUBLE_URL;

    //创建一个static client
    private static AsyncHttpClient clientAsync = new AsyncHttpClient();
    private static SyncHttpClient clientSync = new SyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        Log.i(TAG, "get:" + getAbsoluteUrl(url));
        try {
            clientAsync.get(getAbsoluteUrl(url), params, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        clientAsync.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        params.setContentEncoding(RequestParams.APPLICATION_JSON);
        clientAsync.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(Context content, String url, HttpEntity entity, AsyncHttpResponseHandler responseHandler) {
        Log.i(TAG, "post:" + getAbsoluteUrl(url));
        try {
            clientAsync.setTimeout(120000);
            clientAsync.post(content, getAbsoluteUrl(url), entity, "application/json", responseHandler);
            clientAsync.post(content, getAbsoluteUrl(url), entity, "application/json", responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void post2(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        Log.i(TAG, "post2:" + getAbsoluteUrl(url));
//        clientAsync.addHeader(HTTP.CONTENT_TYPE,
//                "multipart/form-data");
//        clientAsync.addHeader(HTTP.CONTENT_TYPE,
//                "application/octet-stream");
        clientAsync.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void getSync(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        clientSync.get(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

//    private static String getTroubleAbsoluteUrl(String relativeUrl) {
//        return TROUBLE_URL + relativeUrl;
//    }

//    public static void TroubleGet(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
//        Log.i(TAG, "TroubleGet:" + getTroubleAbsoluteUrl(url));
//        clientAsync.get(getTroubleAbsoluteUrl(url), params, responseHandler);
//    }
//
//    public static void post2(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
//        Log.i(TAG, "post2:" + getAbsoluteUrl(url));
////        clientAsync.addHeader(HTTP.CONTENT_TYPE,
////                "multipart/form-data");
////        clientAsync.addHeader(HTTP.CONTENT_TYPE,
////                "application/octet-stream");
//        clientAsync.post(getTroubleAbsoluteUrl(url), params, responseHandler);
//    }
}
